<?php
/**
 * Created by phpstorem.
 * User: yangakw@qq.com
 * Sign: 猥琐发育，别浪
 * Date: 2018/1/7 0007
 * Time: 下午 1:29
 */

namespace app\common\model;


use think\Model;

class Users extends Model
{

    /**
     * 根据id获取用户信息
     * @param $id
     * @return array|false|\PDOStatement|string|Model
     */
    static function get_user_by_id($id)
    {
        $s = new self();
        $data = $s->where(["id"=>$id])->find();
        return $data;
    }

    /**
     * 监测用户名或手机号是否存在
     * @param $username
     * @return null|static
     */
    static function get_user($username)
    {
        $user = Users::get(["user_name|telephone" => $username]);
        return $user;
    }

    /**
     * 注册
     * @param $user_name
     * @param $telephone
     * @param $pwd
     */
    static function register($user_name, $telephone, $pwd)
    {
        Users::create([
            "user_name" => $user_name,
            "telephone" => $telephone,
            "created_time" => date("Y-m-d H:i:s"),
            "password" => $pwd
        ]);
    }

    /**
     * 账户资金
     * @return bool|null|static|UserFund
     */
    function getUserFund()
    {
        if ($this->id) {
            BEGIN:
            $fund = UserFund::get(["user_id" => $this->id]);
            if (empty($fund)) {
                UserFund::create(["user_id" => $this->id]);
                goto BEGIN;
            }
            return $fund;
        }
        return false;
    }

    /**
     * 获取会员列表
     * @param array $cond 查询条件
     * @param array $param 参数判断
     * @return \think\Paginator
     */
    static function list_($param = [], $cond = [])
    {
        if (isset($param["search_"]) && !empty($param["search_"])) {   #用户搜索条件
            $cond["user_name|telephone"] = ["LIKE", "%" . $param["search_"] . "%"];
        }
        $c = new self();
        $r_data = $c->where($cond)->paginate(30, false, ['query' => request()->param()]);
        return $r_data;
    }

    /**
     * 清空cid
     * @param $cid
     */
    static function clear_client_id($cid)
    {
        $me = new self();
        $me->where(["client_id" => $cid])->update(["client_id" => ""]);
    }
}